import {observable as o, computed as c, action as a} from 'mobx';
import _ from 'lodash';
import axios from 'core/axios';
import RootStore from 'core/Store.js';
import request from 'core/request.js';
import Im from 'immutable';
import {browserHistory} from 'react-router';

class Store extends RootStore {
    constructor(args) {
        super(args);
    }

    @o modelsList = [];
    @o modelsTotal = 0;
    @o firstRequest = false;
    @o currentPage = 1;
    @o type = '';
    @o keyword = '';
    @o recordKeyword = '';
    @o hiddenState = '';

    will() {

    }

    closeHandle() {
        this.recordKeyword = '';
        this.getModelsList(true, 1);
    }

    searchHandle() {
        this.recordKeyword = this.keyword;
        this.getModelsList(true, 1)
    }

    keywordChange(v) {
        this.keyword = v;
    }

    keydownMethod(e) {
        if (e.which === 13) {
            this.recordKeyword = this.keyword;
            this.getModelsList(true, 1);
        }
    }

    typeChange(v) {
        this.type = v;
        this.recordKeyword = this.keyword;
        this.getModelsList(true, 1);
    }

    hiddenStateChange(v) {
        this.hiddenState = v;
        this.recordKeyword = this.keyword;
        this.getModelsList(true, 1);
    }

    paginationChange(page) {
        this.getModelsList(true, page);
    }

    getModelsList(loading, page) {
        loading && this.stores.Frame.showLoading();
        this.currentPage = !!page ? page : this.currentPage;
        if (this.recordKeyword !== this.keyword) {
            this.keyword = this.recordKeyword;
        }
        request({
            url: $$.api('getModelsList'),
            method: 'get',
            data: {
                pageNumber: this.currentPage,
                pageSize: $$.SEM.MODELS_LIST_SIZE,
                hidden: this.hiddenState || null,
                type: this.type || null,
                key: this.keyword || null,
            }
        }).then(res => {
            this.firstRequest = true;
            this.stores.Frame.hideLoading();
            this.modelsList = res.data.list;
            this.modelsTotal = res.data.total;
        }).catch(err => {
            this.stores.Message.showMessage(2, err.message);
        })
    }

    previewClick(item) {
        let rOrigin = !!~global.window.location.origin.indexOf('manage-test') ? $$.SEM.STAGING_HREF_ORIGIN : $$.SEM.WWW_HREF_ORIGIN;
        global.window.open(rOrigin + '/editor/' + item.id + '/model');
    }

    editClick(item) {
        this.stores.ModelsDialog.fire('modelDialog:show', item);
    }

    sortClick(item) {
        this.stores.SortDialog.fire('sortDialog:show', {
            sort: item.sort,
            confirmFunc: (sort) => {
                this.stores.Frame.showLoading();
                request({
                    url: $$.api('modelsSort'),
                    method: 'post',
                    data: {
                        sort: sort,
                        id: item.id
                    }
                }).then(res => {
                    this.stores.Message.showMessage(1, res.message);
                    this.getModelsList();
                }).catch(err => {
                    this.stores.Frame.hideLoading();
                    this.stores.Message.showMessage(2, err.message);
                })
            }
        })
    }


    configClick(item) {
        this.stores.ModelsConfig.fire('modelsConfig:show', item);
    }

    deleteClick(item) {
        this.stores.ConfirmDialog.fire('confirmDialog:show', {
            tips: '是否要删除该展馆?',
            func: () => {
                this.stores.Frame.showLoading();
                request({
                    url: $$.api('modelsDelete'),
                    data: {id: item.id}
                }).then(res => {
                    if (this.modelsList.length === 1) { //当前页就剩最后一条
                        this.currentPage = this.currentPage - 1 > 0 ? this.currentPage - 1 : 1;
                    }
                    this.getModelsList();
                    this.stores.Message.showMessage(1, res.message);
                }).catch(err => {
                    this.stores.Frame.hideLoading();
                    this.stores.Message.showMessage(2, err.message);
                });
            }
        })
    }

    modelAdd() {
        this.stores.ModelsDialog.fire('modelDialog:show');
    }

    mount() {
        this.getModelsList();
        this.stores.ModelsDialog.on('modelConfig:update', () => {
            this.getModelsList(true);
        })
    }

}

export default Store;